<?php
/*
  [EWEISHOP] Copyright(C) 2017 Elinkint Inc.
  This is NOT a freeware, use is subject to license terms
   
  OpenPlatformCommon.php 2018-05-29 16:15 likexin
   
*/

namespace common\library\wechat\openplatform;

use common\library\wechat\WechatApi;
use common\component\wechat\Wechat;
use EasyWeChat\OpenPlatform\Auth\VerifyTicket;

class OpenPlatform extends WechatApi
{

    /**
     * 定义接口类型
     * @var string
     */
    static $apiType = 'openPlatform';

    /**
     * 获取授权URL
     * @param string $callbackUrl 回调URL
     * @return array|bool|void
     * @link https://www.easywechat.com/docs/master/zh-CN/open-platform/index
     */
    public static function getPreAuthorizationUrl(string $callbackUrl)
    {
        try {
            $result = self::getInstance()->getPreAuthorizationUrl($callbackUrl);
        } catch (\Exception $result) {}

        return Wechat::apiError($result);
    }

    /**
     * 使用授权码换取接口调用凭据和授权信息
     * @param string|null $authCode 不传的时候会获取 url 中的 auth_code 参数值
     * @return array|bool|void
     * @link https://www.easywechat.com/docs/master/zh-CN/open-platform/index
     */
    public static function handleAuthorize(string $authCode = null)
    {
        try {
            $result = self::getInstance()->handleAuthorize($authCode);
        } catch (\Exception $result) {}

        return Wechat::apiError($result);
    }

    /**
     * 获取授权方的帐号基本信息
     * @param string $appId 目标公众号APPID
     * @return array|bool|void
     * @link https://www.easywechat.com/docs/master/zh-CN/open-platform/index
     */
    public static function getAuthorizer(string $appId)
    {
        try {
            $result = self::getInstance()->getAuthorizer($appId);
        } catch (\Exception $result) {}
        return Wechat::apiError($result);
    }

    /**
     * 获取授权方的选项设置信息
     * @param string $appId 目标公众号APPID
     * @param string $name 设置项名称
     * @return array|bool|void
     * @link https://www.easywechat.com/docs/master/zh-CN/open-platform/index
     */
    public static function getAuthorizerOption(string $appId, string $name)
    {
        try {
            $result = self::getInstance()->getAuthorizerOption($appId, $name);
        } catch (\Exception $result) {}

        return Wechat::apiError($result);
    }

    /**
     * 设置授权方的选项信息
     * @param string $appId 目标公众号APPID
     * @param string $name 设置项名称
     * @param string $value 设置项内容
     * @return array|bool|void
     * @link https://www.easywechat.com/docs/master/zh-CN/open-platform/index
     */
    public static function setAuthorizerOption(string $appId, string $name, string $value)
    {
        try {
            $result = self::getInstance()->setAuthorizerOption($appId, $name, $value);
        } catch (\Exception $result) {}

        return Wechat::apiError($result);
    }

    /**
     * 获取已授权的授权方列表
     * @param int $offset 查询偏移量
     * @param int $count 查询数量
     * @return array|bool|void
     * @link https://www.easywechat.com/docs/master/zh-CN/open-platform/index
     */
    public static function getAuthorizerList(int $offset = 0, int $count = 500)
    {
        try {
            $result = self::getInstance()->getAuthorizers($offset, $count);
        } catch (\Exception $result) {}

        return Wechat::apiError($result);
    }

    /**
     * 获取RefreshToken
     * @param string $appId
     * @return array|bool|void
     */
    public static function getRefreshToken(string $appId)
    {
        // TODO 此处记录缓存
        $authorizer = self::getAuthorizer($appId);
        if(is_error($authorizer)) {
            return $authorizer;
        }
        if(empty($authorizer['authorization_info']) || empty($authorizer['authorization_info']['authorizer_refresh_token'])) {
            return false;
        }
        return $authorizer['authorization_info']['authorizer_refresh_token'];
    }

    /**
     * 代公众号实现业务
     * @param string $appId 目标公众号APPID
     * @param string|null $refreshToken 目标公众号refreshToken
     * @return array|bool|void
     * @link https://www.easywechat.com/docs/master/zh-CN/open-platform/authorizer-delegate
     */
    public static function officialAccount(string $appId, string $refreshToken = null)
    {
        if(empty($refreshToken)) {
            $refreshToken = self::getRefreshToken($appId);
        }

        try {
            $result = self::getInstance()->officialAccount($appId, $refreshToken);
        } catch (\Exception $result) {}

        return Wechat::apiError($result);
    }

    /**
     * 设置TICKET
     * @param string $ticket 为空则清除
     */
    public static function setVerifyTicket(string $ticket)
    {
        $app = self::getInstance();
        $verifyTicket = new VerifyTicket($app);
        $verifyTicket->setTicket($ticket);
        return true;
    }

}